package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Autowired
    private DishMapper dishMapper;


    @Override
    public PageResult pageQuery(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(dto);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Transactional
    @Override
    public void save(SetmealDTO dto) {
        //1、添加套餐表-返回套餐ID
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.insert(setmeal);

        //2、套餐-商品表关联
        if (dto.getSetmealDishes() != null && dto.getSetmealDishes().size() > 0) {
            dto.getSetmealDishes().forEach(setmealDish -> {
                //设置套餐ID
                setmealDish.setSetmealId(setmeal.getId());

            });
        }
        //3、批量添加套餐-商品关联表，多对多
        setmealDishMapper.batchInsert(dto.getSetmealDishes());

    }

    @Override
    public SetmealVO queryById(Long id) {
        //1、查询套餐表
        Setmeal setmeal = setmealMapper.queryById(id);
        //2、根据套餐id查询关联的商品表
        List<SetmealDish> setmealDishList = setmealDishMapper.queryBySetmealId(id);
        SetmealVO vo = new SetmealVO();
        BeanUtils.copyProperties(setmeal, vo);
        if (setmealDishList != null && setmealDishList.size() > 0) {
            vo.setSetmealDishes(setmealDishList);
        }
        return vo;
    }

    @Transactional
    @Override
    public void update(SetmealDTO dto) {
        //1、修改套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.update(setmeal);
        //2、删除套餐关联的商品
        setmealDishMapper.deleteBySetmealId(dto.getId());
        //3、替换套餐新的商品
        dto.getSetmealDishes().forEach(setmealDish -> {
            //设置套餐ID
            setmealDish.setSetmealId(setmeal.getId());

        });
        setmealDishMapper.batchInsert(dto.getSetmealDishes());
    }

    @Transactional
    @Override
    public void batchDelete(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.queryById(id);
            if (setmeal.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

        });
        setmealMapper.batchDeleteByIds(ids);
        setmealDishMapper.batchDeleteBySetmealIds(ids);
    }

    @Override
    public void enableOrDisable(int status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }



    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
